package com.nuatar.meibi.pubvar;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.nuatar.meibi.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MyImageButton extends LinearLayout {
	public WallPaper mwp;
	private ImageView imageViewbutton;
	ItemClick icks;
	Context ctx;

	public MyImageButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		innit_view(context);	
	}

	public MyImageButton(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		innit_view(context);
	}

	public MyImageButton(Context context,WallPaper wp) {
		super(context);
		ctx=context;
		// TODO Auto-generated constructor stub
		mwp=wp;
		innit_view(ctx);
	}
	public void setWallPaper(WallPaper wps){
		mwp=wps;
		innit_view(ctx);
	}
	@SuppressLint("InlinedApi")
	private void innit_view(Context context) {
		// TODO Auto-generated constructor stub		
		imageViewbutton = new ImageView(context);		
		imageViewbutton.setPadding(0, 0, 0, 0);		
		LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		params.weight=1.0f;
		params.setMargins(0, 0, 0, 0);
		imageViewbutton.setLayoutParams(params);		
		setClickable(false);		
		setFocusable(false);		
		this.setPadding(0, 0, 0, 0);	
		setOrientation(LinearLayout.VERTICAL);		
		addView(imageViewbutton);		
		UpdateImage();
	}

	public void setImage(int id) {
		imageViewbutton.setImageResource(id);
	}

	public void setImage(String bmp) {
		imageViewbutton.setImageBitmap(BitmapFactory.decodeFile(bmp));
	}

	public interface ItemClick {// gridView��itemClick�������
		public void Onclick(View view);
	}
	
	public void setImageBitmap(Bitmap bmp) {
		imageViewbutton.setImageBitmap(bmp);
	}

	public void UpdateImage() {
		// TODO Auto-generated method stub
		if(mwp.ShortImages!=null){
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 1;
			ImagePair ip=mwp.ShortImages[0];
			
			String filePath=PhoneMaster.getFilePathFromId(mwp.id, ".ldt");
			byte[] bs;
			try {
				bs = PhoneMaster.getFileByteArray((new File(filePath)), ip.Seeker, ip.Length);
				InputStream is = new ByteArrayInputStream(bs); 
				if(bs.length>0){
					this.setImageBitmap(Mysizer.CreateScaleBitmap(BitmapFactory.decodeStream(is, null, options)));
				}else{
					imageViewbutton.setImageBitmap(Mysizer.CreateScaleBitmap(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.loads)));
				}

			} catch (FileNotFoundException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}else
		{
			imageViewbutton.setImageBitmap(Mysizer.CreateScaleBitmap(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.loads)));
		}
	}
	@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // For simple implementation, or internal size is always 0.
        // We depend on the container to specify the layout size of
        // our view. We can't really know what it is since we will be
        // adding and removing different arbitrary views and do not
        // want the layout to change as this happens.
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
 
        // Children are just made to fill our space.
        int childWidthSize = getMeasuredWidth();
        int childHeightSize = getMeasuredHeight();
        //高度和宽度一样
        heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }



}